#include"threadpoll.hh"
#include"Task.hh"
#include<iostream>
using std::thread;

ThreadPool::ThreadPool(size_t capa, size_t num)
    :_capa(capa)
    ,_thread()
    ,_queuenum(num)
    ,_queue(num)
     ,_isexit(false)
{

}

void ThreadPool::start()
{
    //创建进程并阻塞
    for(size_t i= 0 ;i<_capa;i++)
    {
        _thread.emplace_back(thread(&ThreadPool::dowork,this));
    }
    return;
}

void ThreadPool::dowork()
{

    while(!_isexit)
    {
        Task pwork=_queue.pop();
        if(pwork)
        {
            pwork();
        }
        else
        {
            std::cout<<" 0 == queuesize "<<std::endl;
        }
    }
}
void ThreadPool::addTask(Task &&rhs)
{
    if(rhs)
        _queue.push(std::move(rhs));
    return;
}

void ThreadPool::wakeup()
{
    _queue.wakeup();

}
//成员函数
void ThreadPool::stop()
{
    while(!_queue.empty())
    {
    }

    std::this_thread::sleep_for(std::chrono::seconds(2));
    wakeup();
    _isexit=true;
    for(size_t i=0;i<_capa;i++)
    {
        _thread[i].join();
    }
}
